#! /usr/bin/python
def fact(n):
  return reduce(lambda x,y:x*y,range(1,n+1),1)
 
def perm(n, s):
  if len(s)==1: return s
  q, r = divmod(n, fact(len(s)-1))
  return s[q] + perm(r, s[:q] + s[q+1:])
 
def check(n):
  s = str(n)
  return int(s[1:4]) % 2 == 0 and int(s[2:5]) % 3 == 0
 
s = 0
for i in range(0,18):
  a = int( perm(i,'4310')+'952867')
  if check(a): s += a
  a = int( perm(i,'6410')+'357289')
  if check(a): s += a
print "Answer to PE43 = ", s
